/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package edu.uts.reekie.designer.execution.actions;

import java.util.List;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IWorkbenchPart;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;

import edu.uts.reekie.designer.Messages;
import edu.uts.reekie.designer.ReekieDesignerPlugin;
import edu.uts.reekie.designer.execution.edit.ViewComponentEditPart;
import edu.uts.reekie.designer.execution.model.*;
import edu.uts.reekie.designer.model.*;



public class SetUnsetComponentStereotypeAction
	extends org.eclipse.gef.ui.actions.SelectionAction
{

//
// Action IDs and Request Names
//
protected static final String
	SERVICE = ViewComponentStereotype.SERVICE,   //$NON-NLS-1$
	ACTIVE = ViewComponentStereotype.ACTIVE,   //$NON-NLS-1$
	USER_INITIATED = ViewComponentStereotype.USER_INITIATED;

protected static String getSetId(String stereotype){
	return getSetRequestName(stereotype);
}

protected static String getUnsetId(String stereotype){
	return getUnsetRequestName(stereotype);
}

protected static String getSetRequestName(String stereotype){ 
	return "Set " + stereotype;
}

protected static String getUnsetRequestName(String stereotype){ 
	return "Unset " + stereotype;
}


Request request;
private String stereotype;
private boolean isSetAction;
private static int count = 0;
private int c;

public SetUnsetComponentStereotypeAction(IWorkbenchPart part, String stereotype, boolean isSetAction) {
	super(part);
	
	this.stereotype = stereotype;
	this.isSetAction = isSetAction;
	this.c = count++;
	// validate the stereotype
	if (
			!stereotype.equals(SERVICE) 
			&&
			!stereotype.equals(USER_INITIATED)
			&&
			!stereotype.equals(ACTIVE)
	) {
		stereotype = "UNKNOWN";
	}
	
	if (isSetAction==true){
		request = new Request(getSetRequestName(stereotype));
		setText(Messages.SetUnsetConceptualViewComponentAction_Set_Label_Prefix + stereotype);
		setToolTipText(Messages.SetUnsetConceptualViewComponentAction_Set_Tooltip_Prefix + stereotype);
		setId(getSetId(stereotype));
		// TODO: change to real icon
		setImageDescriptor(
				ImageDescriptor.createFromFile(ReekieDesignerPlugin.class,"icons/plus.gif")); //$NON-NLS-1$
	} else { // unset action
		request = new Request(getUnsetRequestName(stereotype));
		setText(Messages.SetUnsetConceptualViewComponentAction_Unset_Label_Prefix + stereotype);
		setToolTipText(Messages.SetUnsetConceptualViewComponentAction_Unset_Tooltip_Prefix + stereotype);
		setId(getUnsetId(stereotype));
		// TODO: change to real icon
		setImageDescriptor(
				ImageDescriptor.createFromFile(ReekieDesignerPlugin.class,"icons/minus.gif")); //$NON-NLS-1$
	}
	
	
	setHoverImageDescriptor(getImageDescriptor());
}

protected boolean calculateEnabled() {
	return canPerformAction();
}

private boolean canPerformAction() {
	
	if (getSelectedObjects().isEmpty())
		return false;
	
	boolean expectedValue = !this.isSetAction;
	
	List parts = getSelectedObjects();
	for (int i=0; i<parts.size(); i++){
		Object o = parts.get(i);
		if (!(o instanceof ViewComponentEditPart))
			return false;
		
		ViewComponentEditPart part = (ViewComponentEditPart)o;
		if (!(part.getModel() instanceof ViewComponent))
			return false;
		
		ViewComponent model = (ViewComponent)part.getModel();
		
		if (this.stereotype.equalsIgnoreCase(SERVICE)){
			if (model.isService()!=expectedValue)
				return false;
		} else if (this.stereotype.equalsIgnoreCase(ACTIVE)) {
			if (model.isActive()!=expectedValue)
				return false;
		} else if (this.stereotype.equalsIgnoreCase(USER_INITIATED)){
			if (model.isUserInitiated()!=expectedValue)
				return false;
		} 
		
	}
	return true;
}

private Command getCommand() {
	
	List editparts = getSelectedObjects();
	CompoundCommand cc = new CompoundCommand();
	cc.setDebugLabel("Set/Unset Execution View Component Stereotype");//$NON-NLS-1$
	for (int i=0; i < editparts.size(); i++) {
		EditPart part = (EditPart)editparts.get(i);
		cc.add(part.getCommand(request));
	}
	return cc;
}

public void run() {
	execute(getCommand());
}

}

